package com.zhz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhz.domain.SysMenu;
import com.zhz.domain.SysPrivilege;
import com.zhz.model.RolePrivilegesParam;
import com.zhz.service.SysMenuService;
import com.zhz.service.SysPrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhz.mapper.SysRolePrivilegeMapper;
import com.zhz.domain.SysRolePrivilege;
import com.zhz.service.SysRolePrivilegeService;
import org.springframework.util.CollectionUtils;

/**
 * @author ：zhz
 * @date ：Created in 2020/12/20
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 * @description:
 **/
@Service
public class SysRolePrivilegeServiceImpl extends ServiceImpl<SysRolePrivilegeMapper, SysRolePrivilege> implements SysRolePrivilegeService {

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysPrivilegeService sysPrivilegeService;

    @Autowired
    private SysRolePrivilegeService sysRolePrivilegeService;

    @Override
    public List<SysMenu> findSysMenuAndPrivileges(Long roleId) {
        List<SysMenu> list = sysMenuService.list();//查询所有菜单
        //我们在页面显示的是二级菜单，以及二级菜单包含的权限
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<SysMenu> rootMenus = list.stream()
                .filter(sysMenu -> sysMenu.getParentId() == null)
                .collect(Collectors.toList());//一级菜单
        if (CollectionUtils.isEmpty(rootMenus)) {
            return Collections.emptyList();
        }
        //查询所有的二级菜单
        List<SysMenu> subMenu = new ArrayList<>();
        for (SysMenu rootMenu : rootMenus) {
            subMenu.addAll(getChildMenus(rootMenu.getId(), roleId, list));
        }
        return subMenu;
    }

    /**
     * 查询菜单的子菜单(递归)
     *
     * @param parentId 父菜单的ID
     * @param roleId   当前查询的角色的ID
     * @param sources
     * @return
     */
    private List<SysMenu> getChildMenus(Long parentId, Long roleId, List<SysMenu> sources) {
        List<SysMenu> childs = new ArrayList<>();
        for (SysMenu source : sources) {

            if (source.getParentId() == parentId) {//找儿子
                childs.add(source);
                source.setChilds(getChildMenus(source.getId(), roleId, sources));// 给该儿子设置儿子
                List<SysPrivilege> sysPrivileges = sysPrivilegeService.getAllSysPrivilege(source.getId(), roleId);
                source.setPrivileges(sysPrivileges); // 该儿子可能包含权限
            }
        }
        return childs;
    }

    /**
     * 给角色赋予权限
     *
     * @param rolePrivilegesParam
     * @return
     */
    @Override
    public boolean grantPrivileges(RolePrivilegesParam rolePrivilegesParam) {
        Long roleId = rolePrivilegesParam.getRoleId();//角色ID
        //1、先删除之前该角色的权限
        sysRolePrivilegeService.remove(new LambdaQueryWrapper<SysRolePrivilege>()
                .eq(SysRolePrivilege::getRoleId, roleId));
        // 移除之前的值成功
        List<Long> privilegeIds = rolePrivilegesParam.getPrivilegeIds();
        if (!CollectionUtils.isEmpty(privilegeIds)) {
            List<SysRolePrivilege> sysRolePrivileges = new ArrayList<>();
            for (Long privilegeId : privilegeIds) {
                SysRolePrivilege sysRolePrivilege = new SysRolePrivilege();
                sysRolePrivilege.setRoleId(rolePrivilegesParam.getRoleId());
                sysRolePrivilege.setPrivilegeId(privilegeId);
                sysRolePrivileges.add(sysRolePrivilege);
            }
            //新增的值
            return sysRolePrivilegeService.saveBatch(sysRolePrivileges);
        }
        //2、新增该角色的权限
        return true;
    }

}
